<?php


namespace App\Admin\Controller\System\DataCenter;


use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Service\System\DataMaintainService;
use DI\Attribute\Inject;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\NewbieController;
use support\Request;
use support\Response;

/**
 * Class DataMaintainController
 * @package App\system\Controller\DataCenter
 */
#[Controller(prefix: "system/dataMaintain"), Auth]
class DataMaintainController extends NewbieController {
    #[Inject]
    protected DataMaintainService $service;

    /**
     * 列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("index"), Permission("system:dataMaintain:index")]
    public function index (Request $request): Response {
        return $this->success($this->service->getPageList($request->all()));
    }

    /**
     * 详情
     * @param Request $request
     * @return Response
     */
    #[GetMapping("detailed"), Permission("system:dataMaintain:detailed")]
    public function detailed (Request $request): Response {
        return $this->success($this->service->getColumnList($request->input('table', null)));
    }

    /**
     * 优化表
     * @param Request $request
     * @return Response
     */
    #[PostMapping("optimize"), Permission("system:dataMaintain:optimize")]
    public function optimize (Request $request): Response {
        $tables = $request->input('tables', []);
        return $this->service->optimize($tables) ? $this->success() : $this->error();
    }

    /**
     * 清理表碎片
     * @param Request $request
     * @return Response
     */
    #[PostMapping("fragment"), Permission("system:dataMaintain:fragment")]
    public function fragment (Request $request): Response {
        $tables = $request->input('tables', []);
        return $this->service->fragment($tables) ? $this->success() : $this->error();
    }
}